package com.iflytek.universitymanagement.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iflytek.universitymanagement.config.Constants;
import com.iflytek.universitymanagement.config.RoleEnum;
import com.iflytek.universitymanagement.domain.Account;
import com.iflytek.universitymanagement.domain.Student;
import com.iflytek.universitymanagement.domain.Teacher;
import com.iflytek.universitymanagement.exception.ServiceException;
import com.iflytek.universitymanagement.mapper.TeacherMapper;
import com.iflytek.universitymanagement.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author louyp
 * @since 2024-11-26
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Resource
    TeacherMapper teacherMapper;


    public void add(Teacher teacher) {
        Teacher dbTeacher = teacherMapper.selectByTeacherName(teacher.getName());
        if (ObjectUtil.isNotNull(dbTeacher)) {
            throw new ServiceException("账号已存在");
        }
        if (ObjectUtil.isEmpty(teacher.getPassword())) {
            teacher.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(teacher.getName())) {
            teacher.setName(teacher.getName());
        }
        teacher.setRole(RoleEnum.TEACHER.name());
        teacherMapper.insertTeacher(teacher);
    }




    @Override
    public Account login(Account account) {
        Account dbTeacher = teacherMapper.selectByTeacherName(account.getName());
        if (ObjectUtil.isNull(dbTeacher)) {
            throw new ServiceException("账号不存在");
        }
        if (!account.getPassword().equals(dbTeacher.getPassword())) {
            throw new ServiceException("账号或者密码错误");
        }

        return dbTeacher;
    }

    @Override
    public void register(Account account) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(account, teacher);
        add(teacher);
    }



}
